package com.cloudcc.mobile.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.cloudcc.cloudframe.adapter.CommonAdapter;
import com.cloudcc.cloudframe.adapter.ViewHolder;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.approval.TaskApprovalHistoryModel;
import com.cloudcc.mobile.entity.approval.TaskApprovalHistoryModel.Instancehis;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.util.ViewUtils;
import com.cloudcc.mobile.view.activity.Myinformation;
import com.mypage.utils.SaveTemporaryData;

public class TmVerticalTimelineAdapter extends
		CommonAdapter<TaskApprovalHistoryModel.Instancehis> {

	private String mEn = RunTimeManager.getInstance().getlanguage();
	public String id;
	public TmVerticalTimelineAdapter(Context mContext) {
		super(mContext);
	}

	@Override
	public void convert(ViewHolder holder, TaskApprovalHistoryModel.Instancehis t) {

//		for (int j = 0;j<t.getInstancehis().size();j++){
			ViewText viewtext = new ViewText();
			viewtext.textview1 = holder.getView(R.id.tv_operation_man);
			viewtext.textview2 = holder.getView(R.id.tv_operation_state);
			viewtext.textview3 = holder.getView(R.id.tv_operation_time);
			int i = holder.getPostion();
			if (i == 0) {
				holder.getView(R.id.vt_view1).setVisibility(View.INVISIBLE);
			} else if (i == getCount() - 1) {
				holder.getView(R.id.vt_view2).setVisibility(View.INVISIBLE);
			}


		 final Instancehis instancehis = t;
		String stepStatus = instancehis.stepStatus;

		//服务器不判断让客户端自己做中英文判断
		if(!"en".equals(RunTimeManager.getInstance().getlanguage())){//中文状态
			if(stepStatus.equals("Started")){
				stepStatus = "已提交";
			}if(stepStatus.equals("Pending")){
				stepStatus = "审批中";
			}if(stepStatus.equals("Rejected")){
				stepStatus = "审批拒绝";
			}if(stepStatus.equals("Approved")){
				stepStatus = "审批通过";
			}if(stepStatus.equals("Recalled")){
				stepStatus = "已调回";
			}if(stepStatus.equals("ReAssign")){
				stepStatus = "已重新分配";
			}
		}

		String comments = instancehis.comments;
		if (comments == null || comments.equals("null")) {
			comments = "";
		}
		if (stepStatus.equals("Started") || stepStatus.equals("已提交")) {
			ViewUtils.setText(viewtext.textview1, mContext.getString(R.string.faqiren1)
					+ instancehis.actualName);
//			ViewUtils.setText(viewtext.textview2, mContext.getString(R.string.faqineirong1) + comments);
			ViewUtils.setText(viewtext.textview3, mContext.getString(R.string.faqishijian1)
					+ instancehis.lastModifyDate);
			viewtext.textview1.setTextColor(mContext.getResources().getColor(R.color.shenpihuise));
			String neirong = mContext.getString(R.string.faqiren1)+instancehis.actualName;
			SpannableString ss = new SpannableString(mContext.getString(R.string.faqiren1)+instancehis.actualName);
			ss.setSpan( new ForegroundColorSpan(mContext.getResources().getColor(R.color.shenpifaqi)), 4, neirong.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			viewtext.textview1.setText(ss);
			viewtext.textview1.setTextSize(15);
//			viewtext.textview2.setTextSize(15);
			viewtext.textview3.setTextSize(15);
//			viewtext.textview2.setTextColor(mContext.getResources().getColor(R.color.shenpihuise));
			viewtext.textview3.setTextColor(mContext.getResources().getColor(R.color.shenpihuise));
			conut = conut+3;
			viewtext.textview2.setVisibility(View.GONE);
			id = instancehis.userid;
			viewtext.textview1.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					SaveTemporaryData.General = false;
					SaveTemporaryData.GeneralOne = true;
					SaveTemporaryData.mSmart = "";
					SaveTemporaryData.detailDyamic = "";
					Intent intent = new Intent(mContext, Myinformation.class);
					intent.putExtra("userId",instancehis.userid);
					intent.putExtra("name",instancehis.actualName);
					mContext.startActivity(intent);
				}
			});
			holder.setSrc(R.id.vt_imageview, R.drawable.ovalwright);
			holder.getView(R.id.approLine).setVisibility(View.GONE);
		} else if (stepStatus.equals("Pending") || stepStatus.equals("审批中")) {
			ViewUtils.setText(viewtext.textview1, mContext.getString(R.string.daishenpiren1)
					+ instancehis.actualName);

			String neirong1 = mContext.getString(R.string.shenpizhaungtai) + stepStatus;
			SpannableString ss1 = new SpannableString(mContext.getString(R.string.shenpizhaungtai) + stepStatus);
			ss1.setSpan( new ForegroundColorSpan(mContext.getResources().getColor(R.color.shenpizhong)), 5, neirong1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			viewtext.textview2.setText(ss1);

			String neirong2 = mContext.getString(R.string.daishenpiren1) + instancehis.actualName;
			SpannableString ss2 = new SpannableString(mContext.getString(R.string.daishenpiren1) + instancehis.actualName);
			ss2.setSpan( new ForegroundColorSpan(mContext.getResources().getColor(R.color.shenpihei)), 5, neirong2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			viewtext.textview1.setText(ss2);
//			ViewUtils.setText(viewtext.textview3, instancehis.comments==null?mContext.getString(R.string.beizhushenpi):mContext.getString(R.string.beizhushenpi)+ instancehis.comments);

//			viewtext.textview1.setTextColor(mContext.getResources().getColor(R.color.shenpihuise));
//			viewtext.textview1.setTextColor(mContext.getResources().getColor(R.color.daishenpiren));
//			ViewUtils.setText(viewtext.textview2, mContext.getString(R.string.shenpizhaungtai) + stepStatus);
//			viewtext.textview2.setTextColor(mContext.getResources().getColor(R.color.shenpizhuangtai));
			holder.setSrc(R.id.vt_imageview, R.drawable.oval);
			holder.getView(R.id.tv_operation_time).setVisibility(View.GONE);
			conut = conut+3;
//			viewtext.textview1.setOnClickListener(new View.OnClickListener() {
//				@Override
//				public void onClick(View view) {
//					Intent intent = new Intent(mContext, Myinformation.class);
//					intent.putExtra("userId",instancehis.workitemId);
//					intent.putExtra("name",instancehis.actualName);
//					mContext.startActivity(intent);
//				}
//			});
		} else if (stepStatus.equals("Recalled") || stepStatus.equals("调回")) {
			ViewUtils.setText(viewtext.textview1, mContext.getString(R.string.diaohuiren1)
					+ instancehis.actualName);
			if("en".equals(mEn)){
				ViewUtils.setText(viewtext.textview2, mContext.getString(R.string.diaohuizhuangtai1) + stepStatus);
			}else{
				ViewUtils.setText(viewtext.textview2, mContext.getString(R.string.diaohuizhuangtai1) + mContext.getString(R.string.diaohui1));
			}
			
			ViewUtils.setText(viewtext.textview3, mContext.getString(R.string.diaohuishijian1)
					+ instancehis.lastModifyDate);
			conut = conut+3;
		} else {
			ViewUtils.setText(viewtext.textview1, mContext.getString(R.string.shenpiren1)
					+ instancehis.actualName);
			ViewUtils.setText(viewtext.textview2, mContext.getString(R.string.shenpizhaungtai) + stepStatus);
			ViewUtils.setText(viewtext.textview3, mContext.getString(R.string.shenpishijian1)
					+ instancehis.lastModifyDate);
			conut = conut+3;

		}
//		}
	}

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.vertical_timeline;
	}

	class ViewText {
		private TextView textview1;
		private TextView textview2;
		private TextView textview3;
	}
	public int getheight() {
		// TODO Auto-generated method stub
		return conut;
	}
	public String  getId(){
		return id;
	}
}
